package lxt.dao.impl;

import java.util.ArrayList;
import java.util.List;

import lxt.dao.BorrowTruckDao;
import lxt.entity.BorrowTruckInfo;

public class BorrowTruckDaoImpl extends ABseDaoImpl implements BorrowTruckDao {
	
	/**
	 * 根据运输方案表id查询租车信息
	 */
	@SuppressWarnings("unchecked")
	public List<BorrowTruckInfo> queryByInvoiceInfoId(String invoiceInfoId) {
		StringBuffer hql = new StringBuffer("Select tb from BorrowTruckInfo tb where 1=1 ");
		ArrayList<Object> param = new ArrayList<Object>();
		if(invoiceInfoId!=null&&!invoiceInfoId.equals("")){
			hql.append(" And tb.invoiceInfo.id = ?");
			param.add(invoiceInfoId);
		}
		return this.getHibernateTemplate().find(hql.toString(),param.toArray());
	}
	/**
	 * 加载信息
	 */
	@SuppressWarnings("unchecked")
	public List<BorrowTruckInfo> loadBorrowTruck(String truckId, String ownerName,String isPayed,
			int offset, int length) {
		StringBuffer hql = new StringBuffer("Select tc from BorrowTruckInfo tc where 1=1");
		ArrayList<Object> param = new ArrayList<Object>();
		if(truckId!=null&&!truckId.equals("")){
			hql.append(" And tc.truckId like ?");
			param.add("%"+truckId+"%");
		}
		if(ownerName!=null&&!ownerName.equals("")){
			hql.append(" And tc.ownerName like ?");
			param.add("%"+ownerName+"%");
		}
		if(isPayed!=null&&!isPayed.equals("")){
			hql.append(" And tc.isPayed = ?");
			param.add(Integer.parseInt(isPayed));
		}
		return (List<BorrowTruckInfo>) this.queryForPage(hql.toString(), param, offset, length);
	}
	/**
	 * 查询数量
	 */
	public int loadAllBorrowTruckCount(String truckId, String ownerName,String isPayed) {
		StringBuffer hql = new StringBuffer("Select tc from BorrowTruckInfo tc where 1=1");
		ArrayList<Object> param = new ArrayList<Object>();
		if(truckId!=null&&!truckId.equals("")){
			hql.append(" And tc.truckId like ?");
			param.add("%"+truckId+"%");
		}
		if(ownerName!=null&&!ownerName.equals("")){
			hql.append(" And tc.ownerName like ?");
			param.add("%"+ownerName+"%");
		}
		if(isPayed!=null&&!isPayed.equals("")){
			hql.append(" And tc.isPayed = ?");
			param.add(Integer.parseInt(isPayed));
		}
		return this.getAllRowCount(hql.toString(), param);
	}
	/**
	 * 根据派单Id删除数据
	 */
	@SuppressWarnings("unchecked")
	public void deleteOrder(String orderId){
		StringBuffer hql = new StringBuffer(" Select a from BorrowTruckInfo a where a.invoiceInfo.id = ? ");
		ArrayList<Object> param = new ArrayList<Object>();
		param.add(orderId);
		List<BorrowTruckInfo> temp = this.getHibernateTemplate().find(hql.toString(),param.toArray());
		for(int i=0;i<temp.size();i++){
			BorrowTruckInfo entity = temp.get(i);
			this.getHibernateTemplate().delete(entity);
		}
	}
}
